﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using log4net;
using PowerPoint = Microsoft.Office.Interop.PowerPoint;
using Excel = Microsoft.Office.Interop.Excel;
using ThreeByte.ActiveDeck.Config;
using ThreeByte.ActiveDeck.Data.UserResponse;
using ThreeByte.ActiveDeck.Service;
using ThreeByte.ActiveDeck.AddIn.Config;

namespace ThreeByte.ActiveDeck.AddIn.Content.UserResponse {
    class GroupedChartUpdater : UserResponseFeedbackUpdater {

        private static readonly ILog log = LogManager.GetLogger(typeof(ChartUpdater));

        private PowerPoint.Chart MyChart;

        private PowerPoint.ChartData MyChartData;
        private Excel.Workbook MyWorkbook;
        private Excel.Worksheet MyWorksheet;

        private GroupedUserResponseResultsData GroupedUserData;
        private Dictionary<int, float> groupedResults;
        private CoreConfig cConfig;


        public GroupedChartUpdater(string presentation, PowerPoint.Shape shape, WebServiceClient client, GroupedUserResponseResultsData groupData)
            : base(presentation, shape, client, Globals.ThisAddIn.GetCurrentConfig(), true) {

            MyChart = MyShape.Chart;
            MyChartData = MyChart.ChartData;
            
            MyChart.ChartData.Activate();
            MyChart.ApplyDataLabels(PowerPoint.XlDataLabelsType.xlDataLabelsShowLabel, false, false, false, true, false, true, false, false, Type.Missing);
            

            MyWorkbook = (Excel.Workbook)MyChart.ChartData.Workbook;
            MyWorkbook.Application.WindowState = Excel.XlWindowState.xlMinimized;
            MyWorksheet = MyWorkbook.Worksheets[1];

            cConfig = Globals.ThisAddIn.GetCurrentConfig();
           
            GroupedUserData = new GroupedUserResponseResultsData(presentation);

            foreach(PresentationGroup pg in cConfig.PresentationGroups) {
                GroupedUserData.GroupedResults.Add(pg.ID, 0);
            }

            groupedResults = groupData.GroupedResults;
        }

        protected override bool UpdateUserData(System.Xml.Linq.XElement userData) {
            GroupedUserResponseResultsData aggregateGroupedUserData = GroupedUserResponseResultsData.ParseXml(Client.GetMultiQuestionGroupedUserData(cConfig.ID, Pages));

            Dictionary<int, float> newData = aggregateGroupedUserData.GroupedResults;

            return GroupedUserData.UpdateUserResponseResultsData(newData);
        }

        protected override void UpdateGraphic() {
            log.Debug("Updating grouped chart graphic");
            int i = 1;

            foreach(PresentationGroup pg in cConfig.PresentationGroups) {
                string answerCell = string.Format("{0}2", ReportGenerator.GetExcelColumnName(i+1));

                if(GroupedUserData.GroupedResults.ContainsKey(pg.ID)) {
                    MyWorksheet.Range[answerCell].Value = GroupedUserData.GroupedResults[pg.ID];
                } else {
                    MyWorksheet.Range[answerCell].Value = 0;
                }

                i++;
            }            
        }

        public override void Dispose() {
            base.Dispose();
            log.Debug("Closing the workbook");
            MyWorkbook.Close();
        }
    }
}
